################################################################################
# Created By: Pietryka
# Creation Date:  2016-08-22
# Purpose: Create Figure 2, The estimated treatment effects after matching
# Questions: mpietryka@fsu.edu
################################################################################


# PREAMBLE  ====================================



# LOAD PACKAGES --------------
library(tidyverse)


# LOAD DATA  --------------


# created in 'CPS-4A-Matching-Turnout.R'
load("Data/CPS-4A-Matching-Turnout.Rdata")


turnout_list <- list(turnout_est_10,turnout_est_14)

# created in 'CPS-4B-Matching-Participation.R'
load("Data/CPS-4B-Matching-Participation.Rdata")




# Prepare Data ===================================


# TURNOUT  --------------

turnout_estimates <- data_frame(
  year = c(2010L, 2014L),
  est = turnout_list,
  mod = map(est, "att.model"),
  beta = map_dbl(mod, ~ .x["Estimate", "prekkids"]),
  se   = map_dbl(mod, ~ .x["Std. Error", "prekkids"]),
  lb   = beta - 1.96 * se,
  ub   = beta + 1.96 * se,
  outcome_lab = paste(year, "Turnout"))   %>%
  select(-mod, -est)

# PARTICIPATION  --------------

allresults_df <- data_frame(y = outcome_names,
                           mod = map(cps_nonvoting_est_10, "att.model"),
                           year = 2010)  %>%
  mutate(beta = map_dbl(mod, ~ .x["Estimate", "prekkids"]))  %>%
  mutate(se   = map_dbl(mod, ~ .x["Std. Error", "prekkids"]))   %>%
  mutate(lb   = beta - 1.96 * se)   %>%
  mutate(ub   = beta + 1.96 * se)    %>%
  select(-mod)  %>%
  mutate(outcome_lab = c(
    "Discuss Politics",
    "Contact Official",
    "Community Group",
    "Civic Group",
    "Serve as Officer"
  ))  %>%
  bind_rows(turnout_estimates)  %>%
  mutate(dot_type = ifelse(sign(lb) == sign(ub),
                           "statistically insignificant",
                           "statistically significant"))




# PLOT =========================================



# COLORS
color_1 <- "black"
color_2 <- "grey66"
color_insig <- "white"
color_annotate <- "grey40"



the_plot <- ggplot(allresults_df,
                   aes(
                     x = outcome_lab,
                     y = beta,
                     fill = dot_type,
                     label = outcome_lab
                   )) +
  geom_hline(yintercept = 0,
             linetype = 2,
             linewidth = 1.5) +
  geom_pointrange(
    aes(ymin = lb, ymax = ub),
    shape = 21,
    fatten = 12,
    linewidth = 2
  ) +
  geom_text(aes(y = lb),  size = 5, vjust = 1) +
  xlab(NULL) +
  ylab("Effect of parenthood on outcome\n(Difference in means)") +
  theme_minimal() +
  theme(legend.position = "none") +
  theme(text = element_text(size = 16)) +
  theme(axis.text.x = element_blank()) +
  scale_color_manual(values = c("black", "grey66")) +
  scale_fill_manual(values = c("black", "white"))




graphics.off()
windows(10, 6)
the_plot

# Save ==========================================

ggsave("Figures/CPS-Effects-Plot.tiff", compression = "lzw")

